<!DOCTYPE html>
<html lang="en">
<head>
    <!--  name="referrer" content="no-referrer" 这两个属性是解决从git图床获取图片重定向问题如下 -->
    <!--
         请求网址:https://gitee.com/lzh5201314/cool-shark-imgs/raw/master/goods/404ba4ed-ea13-43f0-a4bc-6a72234615c2.jpg
         请求方法:GET
         状态代码:302 Moved Temporarily
         远程地址:180.76.198.77:443
         引荐来源网址政策:strict-origin-when-cross-origin
 -->
    <meta charset="UTF-8" name="referrer" content="no-referrer">
    <title>商品列表</title>
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
    <!-- 引入axios -->
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
    <!-- 引入Vue -->
    <script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
    <!-- import Element UI -->
    <script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
    <link rel="stylesheet" href="css/index.css">
</head>
<body>
<div id="app">
    <el-container>
        <el-header>
            <h1>
                <img src="../imgs/shark.png">
                CoolShark商城管理系统
                <span v-if="" style="float: right;font-size: 15px;">
                    <a href="JavaScript:void(0)" style="text-decoration: none ;font-family: 楷体">退出登录</a>
                </span>
            </h1>
        </el-header>
        <el-container>
            <el-aside>
                <!-- :unique-opened="true":设置子菜单只允许打开一个--->
                <el-menu
                        default-active="4-2"
                        class="el-menu-vertical-demo"
                        :unique-opened="true"
                        @select="handleSelect"
                        @open="handleOpen"
                        @close="handleClose">
                    <el-menu-item index="1">
                        <i class="el-icon-s-home"></i>
                        <span slot="title">首页</span>
                    </el-menu-item>
                    <el-submenu index="2">
                        <template slot="title">
                            <i class="el-icon-user-solid"></i>
                            <span>管理员管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="2-1" v-if="username==='root'">添加管理员</el-menu-item>
                            <el-menu-item index="2-2">管理员列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="3">
                        <template slot="title">
                            <i class="el-icon-s-claim"></i>
                            <span>类别管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="3-1">添加类别</el-menu-item>
                            <el-menu-item index="3-2">类别列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="4">
                        <template slot="title">
                            <i class="el-icon-s-order"></i>
                            <span>商品管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="4-1">添加商品</el-menu-item>
                            <el-menu-item index="4-2">商品列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-submenu index="5">
                        <template slot="title">
                            <i class="el-icon-picture"></i>
                            <span>轮播图管理</span>
                        </template>
                        <el-menu-item-group>
                            <el-menu-item index="5-1">添加轮播图</el-menu-item>
                            <el-menu-item index="5-2">轮播图列表</el-menu-item>
                        </el-menu-item-group>
                    </el-submenu>
                    <el-menu-item index="6">
                        <i class = "el-icon-user-solid"></i>
                        <span>个人信息</span>
                    </el-menu-item>
                </el-menu>
            </el-aside>
            <el-main>
                <el-table :data="tempList"
                          style="width: 100% ;text-align: center">
                    <el-table-column type="index" label="编号"></el-table-column>
                    <el-table-column prop="spuId" label="商品编码"></el-table-column>
                    <el-table-column prop="carousel" label="所属类别"></el-table-column>
                    <el-table-column prop="price" label="价格" width="80"></el-table-column>
                    <el-table-column prop="oldPrice" label="商品原价"></el-table-column>
                    <el-table-column prop="title" label="商品名"
                                     width="180" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="stock" label="库存" width="100"></el-table-column>
                    <el-table-column label="图片">
                        <template slot-scope="scope">
                            <img :src="scope.row.img" alt="" width="100">
                        </template>
                    </el-table-column>
                    <el-table-column prop="enable" label="是否上架" width="150">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.enable"
                                    :active-value="1"
                                    :inactive-value="0"
                                    active-text="启用"
                                    inactive-text="禁用"
                                    active-color="#13ce66"
                                    @change="changeEnable(scope.row)">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="200">
                        <template slot-scope="scope">
                            <el-button type="primary" icon="el-icon-edit" @click="editGoods(scope.row)"
                                       circle></el-button>
                            <el-button type="danger" icon="el-icon-delete" circle
                                       @click="Delete(scope.row)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                        style="text-align: center"
                        :current-page="currentPage"
                        :total="total"
                        :page-sizes="[4, 8, 12, 16]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange">
                </el-pagination>
                <el-dialog title="修改商品详情" :visible.sync="dialogFormVisible">
                    <el-form :model="goods" label-width="80px">
                        <el-form-item label="商品名称">
                            <el-input v-model="goods.title"></el-input>
                        </el-form-item>
                        <el-form-item label="商品库存">
                            <el-input v-model="goods.stock"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格">
                            <el-input v-model="goods.price"></el-input>
                        </el-form-item>
                        <el-form-item label="商品类别">
                            <!--- 类别 -->
                            <el-select v-model="goods.carouselId" placeholder="请选择类别">
                                <el-option v-for="item in carousel" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取 消</el-button>
                        <el-button type="primary" @click="updateGoods">修 改</el-button>
                    </div>
                </el-dialog>
            </el-main>
        </el-container>
    </el-container>
</div>
</body>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return {
                currentPage: 1, // 当前页
                pageSize: 4, // 每页显示条数
                total: 0, // 总条数
                oldPrice: '',
                dialogVisible: false,
                dialogFormVisible: false,
                username: localStorage.getItem('username'),
                goodsArr: [], // 表格元数据
                tempList: [], // 表格显示数据
                carousel: [],
                goodsChangeEnable: {
                    spuId: '',
                    enable: ''
                },
                goods: {
                    spuId: '',
                    title: '',
                    stock: '',
                    price: '',
                    carouselId: '',
                    oldPrice: '',
                },
            }
        },
        methods: {
            changeEnable(goods) {
                v.goodsChangeEnable.spuId = goods.spuId;
                v.goodsChangeEnable.enable = goods.enable;
                console.log(v.goodsChangeEnable.enable);
                axios.post('/goods/changeEnable', v.goodsChangeEnable)
                    .then(response => {
                        let responseBody = response.data;
                        if (responseBody.state === 20000) {
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            axios.get('/goods/list')
                                .then(function (response) {
                                    let responseBody = response.data;
                                    console.log(responseBody.data)
                                    v.goodsArr = responseBody.data;
                                });
                        } else {
                            this.$message.error(responseBody.message);
                        }
                    })
            },
            handleSelect(key, keyPath) {
                console.log(v.username);
                if (key === '1') {
                    location.href = 'index.html';
                } else if (key === '2-1') {
                    location.href = 'AddAdmin.html'; // 跳转到添加管理员页面
                } else if (key === '2-2') {
                    location.href = 'AdminList.html'; // 跳转到管理员列表页面
                } else if (key === '3-1') {
                    let name = prompt("请输入类别名称：", "");
                    if (name === null) {
                        return;
                    }
                    if (name.trim() === '') {
                        v.$message({
                            message: '类别名称不能为空！',
                            type: 'warning'
                        });
                    }
                    axios.get('/admin/insertCarousel?name=' + name)
                        .then(function (response) {
                            v.carousel = response.data.data;
                            location.href = 'CarouselList.html'
                        });
                } else if (key === '3-2') {
                    location.href = 'CarouselList.html'; // 跳转到类别列表页面
                } else if (key === '4-1') {
                    location.href = 'AddGoods.html'
                } else if (key === '5-1') {
                    location.href = 'AddBanner.html'
                } else if (key === '5-2') {
                    location.href = 'BannerList.html'
                }else if (key === '6') {
                    location.href = 'PersonalInfo.html'
                }
            },
            handleOpen(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleClose(key, keyPath) {
                console.log('key = ' + key, 'leyPath = ' + keyPath);
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                if (val >= 4) {
                    v.pageSize = val;
                }
                this.handleCurrentChange(v.currentPage)
                // this.handleCurrentChange(5)
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                v.currentPage = val;
                this.CurrentChangePage(v.goodsArr, v.currentPage);
            },
            CurrentChangePage(list, page) {
                console.log(v.pageSize);
                let from = (page - 1) * v.pageSize;
                let to = page * v.pageSize;
                v.tempList = [];
                for (; from < to; from++) {
                    if (list[from]) {
                        v.tempList.push(list[from]);
                    }
                }
                console.log(v.tempList);
            },
            editGoods(goods) {
                v.dialogFormVisible = true;
                axios.get('/goods/getGoodsBySpuId?spuId=' + goods.spuId)
                    .then(function (response) {
                        v.goods = response.data.data;
                        v.oldPrice = v.goods.price;
                    });
            },
            updateGoods() {
                if (v.goods.price !== v.oldPrice) {
                    v.goods.oldPrice = v.oldPrice;
                }
                axios.post('/goods/update', v.goods)
                    .then(function (response) {
                        let responseBody = response.data;
                        if (responseBody.state === 20000) {
                            v.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            v.dialogFormVisible = false;
                            axios.get('/goods/list')
                                .then(function (response) {
                                    let responseBody = response.data;
                                    console.log(responseBody.data)
                                    v.goodsArr = responseBody.data;
                                });
                        } else {
                            v.$message.error(responseBody.message);
                        }
                    });
            },
            Delete(goods) {
                console.log(goods.spuId)
                axios.get('/goods/delete', {params: {spuId: goods.spuId}})
                    .then(function (response) {
                        let responseBody = response.data;
                        if (responseBody.state === 20000) {
                            v.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            axios.get('/goods/listAll')
                                .then(function (response) {
                                    let responseBody = response.data;
                                    v.goodsArr = responseBody.data;
                                    v.tempList = responseBody.data;
                                    v.total = responseBody.data.length;
                                    v.handleSizeChange(1);
                                    console.log("数据条数：" + v.pageSize)
                                })
                        } else {
                            v.$message.error(responseBody.message);
                        }

                    })
            }
        },
        created: function () {
            axios.get('/goods/listAll')
                .then(function (response) {
                    let responseBody = response.data;
                    v.goodsArr = responseBody.data;
                    v.tempList = responseBody.data;
                    v.total = responseBody.data.length;
                    v.handleSizeChange(1);
                    console.log("数据条数：" + v.pageSize)
                })
            axios.get('/carousel/list')
                .then(function (response) {
                    v.carousel = response.data.data;
                });
        }
    })
</script>
</html>